/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UbahGolonganGUI.java
 *
 * Created on 24 Jul 12, 22:32:37
 */
package gui;

import dao.GolonganDAO;
import entity.Golongan;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Yust
 */
public class UbahGolonganGUI extends javax.swing.JInternalFrame {

    EntityManager em;
    GolonganDAO dao;

    /** Creates new form UbahGolonganGUI */
    public UbahGolonganGUI(EntityManager em) {
        initComponents();
        this.em = em;
        this.binding();
    }

    public void binding() {
        dao = new GolonganDAO(em);
        kode_cmb.removeAllItems();
        for (int i = 0; i < dao.getAll().size(); i++) {
            kode_cmb.addItem(dao.getAll().get(i).getKodeGolongan());



        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        kode_cmb = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        namagolongan_txt = new javax.swing.JTextField();
        gajipokok_txt = new javax.swing.JTextField();
        pajak_txt = new javax.swing.JTextField();
        ubah_btn = new javax.swing.JButton();
        reset_btn = new javax.swing.JButton();
        keluar_btn = new javax.swing.JButton();

        setTitle("Ubah Golongan");

        jPanel1.setBackground(new java.awt.Color(204, 255, 255));

        jLabel1.setText("Kode Golongan");

        kode_cmb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kode_cmbActionPerformed(evt);
            }
        });

        jLabel2.setText("Nama Golongan");

        jLabel3.setText("Gaji Pokok");

        jLabel4.setText("Pajak (dalam %)");

        ubah_btn.setText("Ubah");
        ubah_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubah_btnActionPerformed(evt);
            }
        });

        reset_btn.setText("Reset");
        reset_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reset_btnActionPerformed(evt);
            }
        });

        keluar_btn.setText("Keluar");
        keluar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_btnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(kode_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(namagolongan_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addComponent(gajipokok_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pajak_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ubah_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reset_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(keluar_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(kode_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(namagolongan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(gajipokok_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pajak_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ubah_btn)
                    .addComponent(reset_btn)
                    .addComponent(keluar_btn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void keluar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_btnActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_keluar_btnActionPerformed

    private void reset_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reset_btnActionPerformed
        // TODO add your handling code here:
        //kode_cmb.removeAllItems();
        //this.binding();
        kode_cmb.setSelectedIndex(0);
        
    }//GEN-LAST:event_reset_btnActionPerformed

    private void kode_cmbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kode_cmbActionPerformed
        // TODO add your handling code here:
        String kode = kode_cmb.getSelectedItem().toString();
        namagolongan_txt.setText(dao.getByKode(kode).get(0).getNamaGolongan());
        gajipokok_txt.setText(String.valueOf(dao.getByKode(kode).get(0).getGajiPokok()));
        pajak_txt.setText(String.valueOf(dao.getByKode(kode).get(0).getPajak()));
    }//GEN-LAST:event_kode_cmbActionPerformed

    private void ubah_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubah_btnActionPerformed
        // TODO add your handling code here:
        Golongan g = new Golongan();
        dao = new GolonganDAO(em);
        String message = "";
        if(namagolongan_txt.getText().isEmpty() || gajipokok_txt.getText().isEmpty() || pajak_txt.getText().isEmpty()){
            message = "Data tidak boleh kosong";
            JOptionPane.showMessageDialog(this, message);
        }else{
            try {
                g.setNamaGolongan(namagolongan_txt.getText());
                g.setGajiPokok(Double.parseDouble(gajipokok_txt.getText()));
                g.setPajak(Double.parseDouble(pajak_txt.getText()));
                JOptionPane.showMessageDialog(this, dao.Update(kode_cmb.getSelectedItem().toString(), g));
                
            } catch(NumberFormatException n){
                message = "Gaji dan Pajak harus diisi dengan Angka";
            }catch (Exception e) {
                message = e.getMessage();
                JOptionPane.showMessageDialog(this, message);
            }
        }
    }//GEN-LAST:event_ubah_btnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField gajipokok_txt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton keluar_btn;
    private javax.swing.JComboBox kode_cmb;
    private javax.swing.JTextField namagolongan_txt;
    private javax.swing.JTextField pajak_txt;
    private javax.swing.JButton reset_btn;
    private javax.swing.JButton ubah_btn;
    // End of variables declaration//GEN-END:variables
}
